import Vue from 'vue'
import Vuex from 'vuex'
// import axios from '@/config/axios'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        list: [],
        // classify: [],
        detail: {}
    },
    mutations: {
        setList(state, data) {
            state.list = data
        },
        setDetail(state, id) {
            state.list.forEach(item => {
                item.children.forEach(v => {
                    if (v.id == id) {
                        state.detail = v
                    }
                })
            })
        },
        // 数量增加
        addNum(state, id) {
            state.list.forEach(item => {
                item.children.forEach(v => {
                    if (v.id == id) {
                        v.num++
                    }
                })
            })
        },
        // 数量减少
        reduceNum(state, id) {
            state.list.forEach(item => {
                item.children.forEach(v => {
                    if (v.id == id) {
                        v.num--
                    }
                })
            })
        },
        // 全选
        checkFn(state, allcheck) {
            state.list.forEach(item => {
                item.children.forEach(v => {
                    if (v.num > 0) {
                        v.isCheck = !allcheck
                    }
                })
            })
        }
        // setClassify(state, data) {
        //     state.classify = data
        // }
    },
    actions: {
        getlist(vuex) {
            if (vuex.state.list.length == 0) {
                axios.get('/list', {}).then((res) => {
                    vuex.commit('setList', res.data)
                })
            }
        }

        // getClassify(vuex) {
        //     axios.get('/classifyList', (req, res) => {
        //         vuex.commit('setClassify', res.data)
        //         console.log(res.data);
        //     })
        // }
    },
    modules: {}
})